package com.ruoyi.develop.paySupplierBalance.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.lvBuy.domain.LvBuy;
import com.ruoyi.develop.lvBuy.mapper.LvBuyMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.paySupplierBalance.mapper.PaySupplierBalanceMapper;
import com.ruoyi.develop.paySupplierBalance.domain.PaySupplierBalance;
import com.ruoyi.develop.paySupplierBalance.service.IPaySupplierBalanceService;

/**
 * 供应商收支平衡记录Service业务层处理
 *
 * @author lmm
 * @date 2024-10-31
 */
@Service
public class PaySupplierBalanceServiceImpl implements IPaySupplierBalanceService {
    @Resource
    private PaySupplierBalanceMapper paySupplierBalanceMapper;
    @Resource
    private LvBuyMapper lvBuyMapper;


    /**
     * 查询供应商收支平衡记录
     *
     * @param id 供应商收支平衡记录主键
     * @return 供应商收支平衡记录
     */
    @Override
    public PaySupplierBalance selectById(String id) {
        return paySupplierBalanceMapper.selectById(id);
    }

    /**
     * 查询供应商收支平衡记录列表
     *
     * @param entity 供应商收支平衡记录
     * @return 供应商收支平衡记录
     */
    @Override
    public List<PaySupplierBalance> selectList(PaySupplierBalance entity) {
        return paySupplierBalanceMapper.selectList(entity);
    }

    /**
     * 新增供应商收支平衡记录
     *
     * @param entity 供应商收支平衡记录
     * @return 结果
     */
    @Override
    public int insert(PaySupplierBalance entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        String buyId = entity.getBuyId();
        if (StringUtils.isNotBlank(buyId)) {
            LvBuy lvBuy = lvBuyMapper.selectById(buyId);
            if (lvBuy != null) {
                if (lvBuy.getGoodsType().equals(BusinessConstants.GOODS_TYPE_HARDWARE)){
                    lvBuy.setPayApplyState(BusinessConstants.BUY_PAY_STATUS_PAID);
                }
                if (lvBuy.getPayApplyState().equals(BusinessConstants.GOODS_TYPE_MATERIAL)){
                    lvBuy.setPayApplyState(BusinessConstants.BUY_PAY_STATUS_PAID);
                }
                lvBuyMapper.update(lvBuy);
            }
        }

        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return paySupplierBalanceMapper.insert(entity);
    }

    /**
     * 修改供应商收支平衡记录
     *
     * @param entity 供应商收支平衡记录
     * @return 结果
     */
    @Override
    public int update(PaySupplierBalance entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return paySupplierBalanceMapper.update(entity);
    }

    /**
     * 批量删除供应商收支平衡记录
     *
     * @param ids 需要删除的供应商收支平衡记录主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return paySupplierBalanceMapper.deleteByIds(ids);
    }

    /**
     * 删除供应商收支平衡记录信息
     *
     * @param id 供应商收支平衡记录主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return paySupplierBalanceMapper.deleteById(id);
    }
}
